package dice.sinanya.db.properties.game;

import dice.sinanya.db.tools.DbUtil;
import dice.sinanya.exceptions.ReplyTooShortException;
import dice.sinanya.tools.makedata.ReplyFromXml;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.dom4j.DocumentException;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import static dice.sinanya.system.MessagesSystem.loginInfo;
import static dice.sinanya.tools.getinfo.GetMessagesProperties.entityGame;

/**
 * @author SitaNya
 * 日期: 2019-06-15
 * 电子邮箱: sitanya@qq.com
 * 维护群(QQ): 162279609
 * 有任何问题欢迎咨询
 * 类说明: 查询KP主群类，刷写到静态变量中，只在静态变量中找不到时才需要使用
 */
public class SelectGameProperties {
    private static final Logger log = LogManager.getLogger(SelectGameProperties.class.getName());


    public SelectGameProperties() {
        //        初始化时无需逻辑
    }

    /**
     * 刷新kp主群设定到静态变量中，只有静态变量中找不到某人的kp主群记录时才会使用
     */
    public void flushProperties() {
        try (Connection conn = DbUtil.getConnection()) {
            int i = 0;
            //language=MySQL
            String sql = "select * from test.gameProperties where botId=?";
            try (PreparedStatement ps = conn.prepareStatement(sql)) {
                ps.setString(1, String.valueOf(loginInfo.getLoginId()));
                try (ResultSet set = ps.executeQuery()) {
                    while (set.next()) {
                        entityGame.setJrrpSwitch(set.getBoolean("jrrpSwitch"));
                        entityGame.setJrrpInfo(set.getString("jrrpInfo"));
                        entityGame.setWelcomeSwitch(set.getBoolean("welcomeSwitch"));
                        entityGame.setBotList(set.getBoolean("botList"));
                        entityGame.setDeck(set.getBoolean("deck"));
                        entityGame.setNameSwitch(set.getBoolean("nameSwitch"));
                        entityGame.setOb(set.getBoolean("ob"));
                        entityGame.setSimple(set.getBoolean("simple"));
                        entityGame.setRule(set.getBoolean("rule"));
                        entityGame.setFireNext(set.getString("fireNext"));
                        entityGame.setFireNeedStop(set.getString("fireNeedStop"));
                        entityGame.setFireAbort(set.getString("fireAbort"));
                        entityGame.setFireAgentError(set.getString("fireAgentError"));
                        entityGame.setFireContinue(set.getString("fireContinue"));
                        entityGame.setFireStop(set.getString("fireStop"));
                        entityGame.setFireAlreadyStop(set.getString("fireAlreadyStop"));
                        entityGame.setFireResetNeedStop(set.getString("fireResetNeedStop"));
                        entityGame.setFireReset(set.getString("fireReset"));
                        entityGame.setGetName(set.getString("getName"));
                        entityGame.setObAlreadyInsert(set.getString("obAlreadyInsert"));
                        entityGame.setObInsert(set.getString("obInsert"));
                        entityGame.setObRemove(set.getString("obRemove"));
                        entityGame.setObAlreadyRemove(set.getString("obAlreadyRemove"));
                        entityGame.setObClear(set.getString("obClear"));
                        entityGame.setObShow(set.getString("obShow"));
                        entityGame.setObEmpty(set.getString("obEmpty"));
                        entityGame.setWelcomeClose(set.getString("welcomeClose"));
                        entityGame.setWelcomeOpen(set.getString("welcomeOpen"));
                        entityGame.setWelcomeInsert(set.getString("welcomeInsert"));
                        entityGame.setBgInfo(set.getString("bgInfo"));
                        entityGame.setCocCard7Make(set.getString("cocCard7Make"));
                        entityGame.setCocCard6Make(set.getString("cocCard6Make"));
                        entityGame.setGroupSwitchInfo(set.getString("groupSwitchInfo"));
                        entityGame.setJrrpStart(set.getString("jrrpStart"));
                        entityGame.setJrrpStop(set.getString("jrrpStop"));
                        entityGame.setNpcStart(set.getString("npcStart"));
                        entityGame.setNpcStop(set.getString("npcStop"));
                        entityGame.setWelcomeStart(set.getString("welcomeStart"));
                        entityGame.setWelcomeStop(set.getString("welcomeStop"));
                        entityGame.setGasStart(set.getString("gasStart"));
                        entityGame.setGasStop(set.getString("gasStop"));
                        entityGame.setBgStart(set.getString("bgStart"));
                        entityGame.setBgStop(set.getString("bgStop"));
                        entityGame.setTzStart(set.getString("tzStart"));
                        entityGame.setTzStop(set.getString("tzStop"));
                        entityGame.setSimpleStart(set.getString("simpleStart"));
                        entityGame.setSimpleStop(set.getString("simpleStop"));
                        entityGame.setObStart(set.getString("obStart"));
                        entityGame.setObStop(set.getString("obStop"));
                        entityGame.setDeckStart(set.getString("deckStart"));
                        entityGame.setDeckStop(set.getString("deckStop"));
                        entityGame.setRankCantStart(set.getString("rankCantStart"));
                        entityGame.setRankStart(set.getString("rankStart"));
                        entityGame.setRankStop(set.getString("rankStop"));
                        entityGame.setRuleChange(set.getString("ruleChange"));
                        entityGame.setRuleGet(set.getString("ruleGet"));
                        entityGame.setRuleDefault(set.getString("ruleDefault"));
                        entityGame.setReply(set.getString("reply"));
                        try {
                            new ReplyFromXml(entityGame.getReply()).flushMapInRunning();
                        } catch (DocumentException | ReplyTooShortException e) {
                            e.printStackTrace();
                        }
                        i++;
                    }
                }
                if (i == 0) {
                    new InsertProperties().insertProperties(entityGame);
                }
            }
        } catch (SQLException e) {
            log.error(e.getMessage(), e);
        }
    }
}
